<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ include file="/wesite/view/common/base.jsp" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>历史采购</title>
    <%@ include file="/wesite/view/common/common.jsp" %>
    <script type="text/javascript" src="${webctx}/script/aui-scroll.js"></script>
</head>
<style>
    .page-pno{
        position: fixed;
        bottom:5px;
        left:50%;
        padding:1px .426666rem;
        -webkit-transform: translateX(-50%);
        -moz-transform: translateX(-50%);
        -ms-transform: translateX(-50%);
        -o-transform: translateX(-50%);
        transform: translateX(-50%);
        height:1rem;
        line-height: 1rem;
        text-align: center;
        background-color: #000;
        opacity: .3;
        color: #fff;
        font-size:.586667rem;
        border-radius: 1rem ;
    }
</style>
<body>
<div class="hislist-box" id="hislist-box">
    <ul id="hislistHtml">
    </ul>
    <div id="hislistProductHolder" style=" margin: 10px 0; color: #999; text-align: center;display: none;"></div>
    <div class="page-pno" style="display:none;"><span class="now-page">1</span> / <span class="count-page">24</span></div>
</div>
<%@ include file="/wesite/view/common/popup-menu.jsp" %>
<%@ include file="/wesite/view/common/go-top.jsp" %>
</body>
</html>
<script>
    var dom = document.getElementById("hislist-box");//获取ID
    var win_hig = parseInt($(window).height())/2; //获取屏幕高度的一半
    var index = 1;
    var pno = 1;
    var loginhisList = false;
    var pagecount = 0;
    var startY = endY = 0;
    function hislist() {
        if (loginhisList) {
            return false;
        } else {
            loginhisList = true;
        }
        $("#hislistProductHolder").text("正在加载 ...").show();
        $w.httpRequest({
            url: apiroot + "/we/history",
            data: {"pno": pno, "platform" : 2},
            ok: function (ret) {
                loginhisList = false;
                var datalist = ret.Data;

                var dataitem = ret.Data.list;
                pagecount= datalist.pageCount; //总页数
                console.log(pagecount);
                $(".count-page").text(pagecount);

                if (datalist && dataitem && dataitem.length > 0) {
                    for (var i = 0; i < dataitem.length; i++) {
                        var data = dataitem[i];
                        var amounttotal = data.amount - data.djamount;
                        var h = "";

                        if(index % 12 == 0){
                            h += "<li class='index-for' page='"+ pno +"' sid='"+ pno +"'>"
                        } else {
                            h += "<li>"
                        }
                        h += "<div class='order-num'>" + (index++) + "</div>";
                        h += "<a class='hislist-img' href='${ctx }/we/product/" + data.id + "'>";
                        h += "<img src='" + getRandomFileserver() + data.photo + "' onerror='this.src=\"" + ctx + "/website/images/noimg.jpg\"'>";
                        ;
                        h += " <span class='msk'></span>";
                        h += " <span class='text'>已购" + data.buyamount + "件</span>";
                        h += "</a>";
                        h += "<div class='hislist-text fl'>";
                        h += "<p class='text-name'>" + data.productname + " </p>";
                        h += "<p class='text-company txt'>" + data.b_chanjia + " </p>";
                        h += "<p class='text-st txt'>" + data.b_guige + " </p>";
                        if (data.canbuy ==1 && data.status == 2) {
                            if (data.promotion_flash_sale) {
                                h += "<p class='text-price'>¥" + data.pg_price;
                            } else {
                                h += "<p class='text-price'>¥" + data.price;
                            }
                            if (data.promotion_coupon) {
                                h += ("<em>券</em>");
                            }
                            if (data.promotion_flash_sale) {
                                h += "<em>特价</em>"
                            }
                            if (data.promotion_buy_send) {
                                h += "<em>买送</em>"
                            }
                            if (data.promotion_money_off) {
                                h += "<em>满减</em>"
                            }
                            if (data.promotion_purchase_gift) {
                                h += "<em>满赠</em>"
                            }
                            h += "</p>";
                            if (data.promotion_flash_sale) {
                                if (!data.fs_amount) {
                                    h += "<a class='ico-shopping-cart' href='javascript:void(0);')'><img src='" + ctx + "/wesite/images/ico-shopping-cart-no.png'></a>";
                                }else {
                                    h += "<a class='ico-shopping-cart' href='javascript:;'onclick='stopBubble(event," + data.id + ")'><img src='" + ctx + "/wesite/images/ico-shopping-cart.png'></a>";

                                }
                            } else {
                                if (amounttotal > 0) {
                                    h += "<a class='ico-shopping-cart' href='javascript:;'onclick='stopBubble(event," + data.id + ")'><img src='" + ctx + "/wesite/images/ico-shopping-cart.png'></a>";
                                }else {
                                    h += "<a class='ico-shopping-cart' href='javascript:void(0);')'><img src='" + ctx + "/wesite/images/ico-shopping-cart-no.png'></a>";
                                }
                            }
                        }else if(data.status == 1) {
                            h += "<p class='text-iscontrol'>商品已下架</p>"
                            h += "<a class='ico-shopping-cart' href='javascript:void(0);')'><img src='" + ctx + "/wesite/images/ico-shopping-cart-no.png'></a>";
                        }else {
                            h += "<p class='text-iscontrol'>不在本区域销售</p>"
                            h += "<a class='ico-shopping-cart' href='javascript:void(0);')'><img src='" + ctx + "/wesite/images/ico-shopping-cart-no.png'></a>";
                        }

                        h += "</div>";
                        h += "<div class='cl'></div>";
                        h += "</li>";
                        $("#hislistHtml").append(h);
                    }
                    pno++;
                    $("#hislistProductHolder").hide();
                }else {
                    pno = 0;
                    $("#hislistProductHolder").text("已加载全部");
                }
            }
        })
    }

    var t;
    //touchStart事件（手点到屏幕 触发）
    dom.addEventListener('touchstart', function (event) {
        var touch = event.targetTouches[0];
        startY = touch.clientY;
    }, false);

    //touchmove事件（手指滑动屏幕 触发）
    dom.addEventListener('touchmove', function (event) {
        var touch = event.targetTouches[0];
        endY = touch.clientY;
        clearInterval(t);
        $(".page-pno").fadeIn("slow");
    }, false);

    //touchend事件（手指离开屏幕 触发）
    dom.addEventListener('touchend', function () {
        t = setTimeout("$('.page-pno').fadeOut('slow');", 3000);
    }, false);

    //滚动条事件
    $(window).scroll(function () {
        var top = document.body.scrollTop;//获取滚动条离顶部距离
        var txt_pri = endY - startY;//判断滑动方向 大于0 往上滑  小于0 往下滑
        if(txt_pri < 0){
            var txt = parseInt($(".now-page").text());//当前页码
            if(txt < pagecount){
                var top_h = $("#hislistHtml").find("[page='"+ txt +"']").offset().top - top - win_hig;
            }
            if (top_h < 0){
                txt++;
                $(".now-page").text(txt);
            }
//                console.log("向上滑");
        }else {
            var txt = parseInt($(".now-page").text());//当前页码
            if(txt > 1){
                txt -= 1;
                top_h = $("#hislistHtml").find("[page='"+ txt +"']").offset().top - top - win_hig;

                if (top_h > 0){
                    $(".now-page").text(txt);
                }
            }
//                console.log("向xia滑");
        }
        if (top < 10){
             $(".now-page").text("1");
        }
    })

    $(function () {
        hislist();
        var bottomHeight = $(".cl").height() * 2;
//        console.log(bottomHeight);
        var scroll = new auiScroll({
            listen: true, //是否监听滚动高度，开启后将实时返回滚动高度
            distance: bottomHeight //判断到达底部的距离，isToBottom为true
        }, function (ret) {
            if (ret.isToBottom) {
                if (pno > 0) {
                    hislist();
                }else{
                    $(".now-page").text(pagecount);
                }
            }
        });
    })

</script>